﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）
//【初阶】12.string(2)

//【头文件包含】
//C++的主要头文件
#include<iostream>//c++的输入输出流
using std::cout, std::endl,std::cin;

//使用string需要包含的头文件和引入的类
#include<string>
using std::string;

//C的主要头文件
//#include<stdio.h>
//#include<stdlib.h>
//#include<assert.h>
//#include<string.h> 




//【命名空间】

//【结构体声明】【类声明】



//【函数声明】
void Test1();void Test2();void Test3();
/*url分离*/void split_url(const string& url);
int lengthOfLastWord(string s);
string addStrings(string num1, string num2);
string multiply(string num1, string num2);
//【主函数】
int main()
{
	
	Test1();
	Test2();
	Test3();
	
	cout << "****主函数结束****" << endl;
	return 0;
}

//【函数定义】


/*测试用例*/void Test1()
{

	cout << "****测试用例开始****"<< endl;
	string s1("hello");
	s1 += ' ';
	s1 += "world";
	cout << s1 << endl;//调用string重载的operator<<输出
	cout << s1.c_str() << endl;//直接输出const char*
	for (size_t i = 0; i < s1.size(); ++i)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;

	const char* str = s1.c_str();
	while (*str)
	{
		cout << *str << " ";
		++str;
	}
	cout << endl;

	cout << "****测试用例结束****" << endl << endl;

}
/*url分离*/void split_url(const string& url)
{
	size_t i1 = url.find(':');
	if (i1 != string::npos)
	{
		cout << url.substr(0, i1) << endl;
	}
	size_t i2 = url.find('/', i1 + 3);
	if (i2 != string::npos)
	{
		cout << url.substr(i1 + 3, i2 - i1 - 3) << endl;
	}
	cout << url.substr(i2 + 1) << endl;
}
/*计算最后一个单次长度*/int lengthOfLastWord(string s)
{
	size_t p0 = s.size()-1;
	size_t p1 = s.rfind(' ', p0);
	if (p1 == p0)
	{
		while (p1 != string::npos&&p1==p0)
		{
			--p1;
			p0 = p1;
			p1 = s.rfind(' ', p1);
		}
	}
	return (int)(p0 - p1);
}

/*测试用例*/void Test2()
{
	cout << "****测试用例开始****" << endl;
	string s1("string.cpp");
	string s2("string.c");
	string s3("string.txt");
	size_t pos1=s1.find('.');
	if (pos1 !=string::npos)
	{
		cout << s1.substr(pos1) << endl;
	}
	size_t pos2 = s2.find('.');
	if (pos2 != string::npos)
	{
		cout << s2.substr(pos2) << endl;
	}
	size_t pos3 = s3.find('.');
	if (pos3 != string::npos)
	{
		cout << s3.substr(pos3) << endl;
	}

	string url1("https://www.bilibili.com/video/BV1uF57z7EJk/?spm_id_from=333.1007.tianma.1-2-2.click");
	string url2("http://www.cpta.com.cn/cpta/583.html");
	split_url(url1);
	split_url(url2);

	string s4("hello world");
	int ret = lengthOfLastWord(s4);
	cout << ret << endl;
	

	cout << "****测试用例结束****" << endl<<endl;
}
/*以字符串计算两数字加法*/string addStrings(string num1, string num2)
{
	int end1 = num1.size() - 1;
	int end2 = num2.size() - 1;
	int next = 0;
	string retstr;
	while (end1 >= 0 || end2 >= 0)
	{
		int val1 = 0, val2 = 0;
		if (end1 >= 0)
		{
			val1 = num1[end1] - '0';
		}
		if (end2 >= 0)
		{
			val2 = num2[end2] - '0';
		}
		int ret = val1 + val2 + next;
		if (ret >= 10)
		{
			ret -= 10;
			next = 1;
		}
		else
		{
			next = 0;
		}
		retstr += ret + '0';
		--end1; --end2;
	}
	if (next == 1)
	{
		retstr += '1';
	}
	string strret;
	string::reverse_iterator rit = retstr.rbegin();
	while (rit != retstr.rend())
	{
		strret += *rit;
		++rit;
	}
	return strret;
	/*reverse(retstr.begin(), retstr.end());
	return retstr;*/
}
/*以字符串计算两数字乘法*/string multiply(string num1, string num2)
{

	string one("1");
	string ret("0");
	int end1 = num1.size() - 1;
	int wei = 0;
	while (end1 >= 0)
	{
		string i("0");
		string ret_single("0");
		string n;
		n += num1[end1];
		while (i != n)
		{
			ret_single = addStrings(ret_single, num2);
			

			i = addStrings(i, one);
		}
		for (int j = 0; j < wei; ++j)
		{
			ret_single += '0';
		}
		ret = addStrings(ret, ret_single);
		--end1;
		++wei;
	}

	return ret;
}
/*测试用例*/void Test3()
{
	cout << "****测试用例开始****" << endl;
	string num1("123");
	string num2("100");
	cout << addStrings(num1, num2) << endl;
	cout << multiply(num1, num2) << endl;

	cout << "****测试用例结束****" << endl << endl;
}

//【笔记】
//【初阶】12.string(2)
	//c_str:返回c形式的字符串
		//返回字符串数组的指针
		//如果字符串中有自行添加的\0,那用该方式调用返回字符串会被\0截断
	//find查找段字符串的内容(默认从0位置开始)
		//返回size_t类型，找到返回下标没有返回string::nops = -1；
	//rfind 
		//反向查找从后向前找 
	//substr：从pos位置取len个字符，不给长度则取到末尾
	//string支持比较大小
	//getline可以接受一整行的内容直到遇到换行符停止 getline（cin,被接收方）


